define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'learn/livecourse/index' + location.search,
                    add_url: 'learn/livecourse/add',
                    edit_url: 'learn/livecourse/edit',
                    del_url: 'learn/livecourse/del',
                    chapteradd_url: 'learn/livecourse/chapteradd',
                    chapteredit_url: 'learn/livecourse/chapteredit',
                    chapterdel_url: 'learn/livecourse/chapterdel',
                    multi_url: 'learn/livecourse/multi',
                    table: 'learn_livecourse',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'sort', title: __('序号')},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'series.name', title: __('系列名称'), operate:'LIKE'},
                        {field: 'start_time', title: __('课程开始时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('课程结束时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 't_name', title: __('主讲老师'), operate:'LIKE'},
                        {field: 'introduce', title: __('老师简介'), operate:false},
                        //{field: 'origin_price', title: __('Origin_price'), operate:'BETWEEN'},
                        //{field: 'price', title: __('Price'), operate:'BETWEEN'},
                        //{field: 'visitor_origin_price', title: __('Visitor_origin_price'), operate:false},
                        //{field: 'visitor_price', title: __('Visitor_price'), operate:false},
                        //{field: 'buy_numbers', title: __('Buy_numbers')},
                        //{field: 'image', title: __('Image'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'thumbs', title: __('Thumbs'), operate:'>='},
                        //{field: 'status', title: __('Status'), operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            //buttons: [
                            //    {
                            //        name: 'chapter',
                            //        title: __('直播室管理'),
                            //        classname: 'btn btn-xs btn-primary btn-addtabs btn-ajax',
                            //        icon: 'fa fa-folder-o',
                            //        url: 'learn/livecourse/chapter'+ location.search
                            //        // url: 'learn/chapter/index'+ location.search
                            //    }
                            //],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        chapter: function () {
            Table.api.init({
                extend: {
                    chapter_url: 'learn/livecourse/chapter'+ location.search,
                    table: 'learn_livecourse_chapter'
                }
            });
            var table1 = $('#table1');

            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'user.username', title: __('主持人'), operate:'LIKE'},
                        {field: 'talk.username', title: __('主讲人'), operate:'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'file', title: __('File'), operate:false},
                        {field: 'is_show_ppt', title: __('Is_show_ppt'), searchList: {"0":__('Is_show_ppt 0'),"1":__('Is_show_ppt 1')}, formatter: Table.api.formatter.normal},
                        {field: 'operate',
                            title: __('Operate'),
                            table: table1,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'chapteredit',
                                    title: __('课程聊天室编辑'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'learn/livecourse/chapteredit',
                                    extend: 'data-area=["100%","100%"]',
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'chapterdel',
                                    title: __('Delete'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-trash',
                                    click: function (data,row) {
                                        confirmf(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                },
                            ],
                            operate:false,
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            var confirmf = function(id){
                Layer.confirm("确定删除该课程聊天室吗？",
                    {
                        title: '删除课程章节',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(id,index, layero);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            }
            var submitExportForm = function (id,index, layero) {
                var data = {
                    ids: id
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('learn/livecourse/chapterdel'),
                    data: data,
                    pk: 'id'
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    $(".btn-refresh").trigger("click");
                });
            };
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table1);  //所有选中的项
                Layer.confirm("确定删除选中的"+ ids.length +"个课程聊天室吗？",
                    {
                        title: '批量删除课程聊天室',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(ids.join(","), layero, index);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            });
        },
        chapteradd: function () {
            Controller.api.bindevent();
        },
        chapteredit: function () {
            Controller.api.bindevent();
        },
        chapterdel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }

    };
    return Controller;
});